﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Security;
using Core.DAL;
using Core.Utils;

namespace Core.BOL
{
    public class CommentRepository
    {
        private ScoutNetDataContext context = null;

        #region Construtor
        public CommentRepository()
        {
            context = GeneralUtils.GetDataContext();
        }
        #endregion

        #region Methodes
        public void InsertComment(tSite_Photos_Comment comment)
        {
            context.tSite_Photos_Comments.InsertOnSubmit(comment);
        }

        public List<tSite_Photos_Comment> GetCommentsByImageID(int ID)
        {
            List<tSite_Photos_Comment> query = (from n in context.tSite_Photos_Comments
                                                join u in context.aspnet_Users on n.UID equals u.UserId
                                                where n.IID == ID
                                                orderby n.InsertDate descending
                                                select n).ToList();
            return query;
        }

        public void ArchiveUserComments(Guid UID)
        {
            List<tSite_Photos_Comment> lComments = (from c in context.tSite_Photos_Comments
                                                    where c.UID == UID
                                                    select c).ToList();

            MembershipUser mu = Membership.GetUser(ConfigurationManager.AppSettings["AnonymeUser"]);
            if (mu != null)
            {
                foreach (tSite_Photos_Comment comments in lComments)
                {
                    comments.UID = (Guid)mu.ProviderUserKey;
                }
            }
        }

        public void DeletePictureCommentsByAlbumID(int AID)
        {
            List<tSite_Photos_Comment> c = context.tSite_Photos_Comments.Where(p => p.AID == AID).ToList();
            if (c != null && c.Count > 0)
            {
                context.tSite_Photos_Comments.DeleteAllOnSubmit(c);
            }
        }

        public void DeletePictureCommentsByImageID(int IID)
        {
            List<tSite_Photos_Comment> c = context.tSite_Photos_Comments.Where(p => p.IID == IID).ToList();
            if (c != null && c.Count > 0)
            {
                context.tSite_Photos_Comments.DeleteAllOnSubmit(c);
            }
        }

        public void Save()
        {
            context.SubmitChanges();
        }
        #endregion
    }
}
